package top.ljl.crawler.service.impl;

import org.json.JSONArray;
import org.json.JSONObject;
import com.alibaba.fastjson.JSON;
import top.ljl.crawler.Entity.Data;
import top.ljl.crawler.utils.GetJson;
import top.ljl.crawler.service.CrawlerService;

import java.util.ArrayList;
import java.util.List;


/**
 * @ClassName
 * @Description TODO
 * @Author L1470
 * @Date 2021/11/3 19:34
 * @Version 1.0
 **/
public class CrawlerServiceImpl implements CrawlerService {
    private String crawler_url = "http://da.wa.news.cn/nodeart/page";

    private final String CRAWAL_PARM = "?nid=11227931&pgnum=";

    private GetJson getJson = new GetJson();


    @Override
    public List<Data> getDataList(Integer pageNumber) throws Exception {
        if (pageNumber >= 0 && pageNumber <= 10000) {
            String address = crawler_url + CRAWAL_PARM + pageNumber;
            //获取json对象数据
            JSONObject httpJson = getJson.getHttpJson(address);
            if (httpJson != null) {
                //取出json数据数组
                JSONObject data1 = httpJson.getJSONObject("data");
                JSONArray dataArray = data1.getJSONArray("list");
                if (dataArray != null) {
                    //将json数组转化为List
                    List<Data> dataList = JSON.parseArray(dataArray.toString(), Data.class);
                    for (Data data : dataList) {
                        System.out.println(data);
                    }
                    return dataList;
                } else {
                    return new ArrayList<>();
                }
            } else {
                return new ArrayList<>();
            }
        }
        return new ArrayList<>();
    }
}
